<template>
  <ContentPanel :showTopBorder="true">
    <div class="top">
      <div class="number">我的好友({{ contactList.length }})</div>
      <div class="search">
        <el-form-item>
          <el-input
            placeholder="输入名称搜索"
            size="small"
            v-model="searchKey"
            clearable
          />
        </el-form-item>
      </div>
    </div>
    <div
      class="contact_list"
      v-for="item in contactList"
      v-show="item.contactName.includes(searchKey)"
    >
      <div class="contact">
        <div class="avatar-name">
          <Image :src="item.contactIcon" />
          <span class="name">{{ item.contactName }}</span>
        </div>
        <div class="btn">
          <el-dropdown placement="bottom-end">
            <span class="el-dropdown-link">
              <div class="iconfont icon-gengduo"></div>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="sendMessage(item.contactId)"
                  >发送信息</el-dropdown-item
                >
                <el-dropdown-item @click="friendDetail(item.contactId)"
                  >查看详情</el-dropdown-item
                >
                <el-dropdown-item @click="addContactBlack(item.contactId)"
                  >拉黑好友</el-dropdown-item
                >
                <el-dropdown-item @click="deleteContact(item.contactId)"
                  >删除好友</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
  </ContentPanel>
  <!--详情-->
  <FriendDetail ref="friendDetailRef"></FriendDetail>
</template>

<script setup>
import FriendDetail from "./FriendDetail.vue";
import { ref, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
const { proxy } = getCurrentInstance();
const router = useRouter();
import { useFriendDeleteVisibleStore } from "@/stores/FriendDeleteStore";
import { openIndexedDB, getDataByKey, deleteDB, cursorDelete } from "@/utils/IndexedDB";
const friendDeleteStore = useFriendDeleteVisibleStore();

const searchKey = ref("");
const contactList = ref([]);
const loadContact = async () => {
  const res = await proxy.Api.queryUserContactList();
  if (res.code !== 200) {
    proxy.$message.error(res.message);
    return;
  } else {
    contactList.value = res.data;
  }
};
loadContact();

const sendMessage = (contactId) => {
  router.push({
    path: "/chat",
    query: { contactId: contactId },
  });
};

const friendDetailRef = ref();
const friendDetail = async (contactId) => {
  const params = {
    contactId: contactId,
  };
  const res = await proxy.Api.queryContactUserInfo(params);
  if (res.code !== 200) {
    proxy.$message.error(res.messgae);
    return;
  } else {
    friendDetailRef.value.show(res.data);
  }
};

const params = ref({});
const addContactBlack = (contactId) => {
  proxy.Confirm({
    message: "确定要将该联系人拉黑吗？",
    okfun: async () => {
      params.value.contactId = contactId;
      const res = await proxy.Api.blackedContact(params.value);
      if (res.code !== 200) {
        proxy.$message.error(res.message);
        return;
      } else {
        proxy.$message.success("已拉黑");
        loadContact();
        deleteChatAndMessage(contactId);
      }
    },
  });
};

const deleteContact = (contactId) => {
  proxy.Confirm({
    message: "确定要将该联系人删除吗？",
    okfun: async () => {
      params.value.contactId = contactId;
      const res = await proxy.Api.deleteContact(params.value);
      if (res.code !== 200) {
        proxy.$message.error(res.message);
        return;
      } else {
        proxy.$message.success("已删除");
        loadContact();
        deleteChatAndMessage(contactId);
      }
    },
  });
};

const deleteChatAndMessage = async (contactId) => {
  let chatDb = await openIndexedDB(
    "chat" + JSON.parse(localStorage.getItem("userInfo")).userId
  );
  const existChatUser = await getDataByKey(chatDb, "chatUser", contactId);
  deleteDB(chatDb, "chatUser", contactId);
  cursorDelete(chatDb, "message", "chatId", existChatUser.chatId);
  friendDeleteStore.setFriendDelete(existChatUser);
};
</script>

<style lang="scss" scoped>
.top {
  margin-top: 5px;
  display: flex;
  justify-content: space-between;
  border-radius: 5px;
  background: #fff;
  height: 70px;

  .number {
    margin-left: 5px;
    margin-top: 25px;
    font-weight: 600;
    font-size: 13px;
  }

  .search {
    margin-right: 5px;
    margin-top: 25px;

    .el-input {
      width: 200px;

      .iconfont {
        cursor: pointer;
      }
    }
  }
}

.contact_list {
  padding: 5px;
  background: #fff;
  border-radius: 5px;
  margin-top: 5px;
  height: 60px;

  .contact {
    display: flex;
    justify-content: space-between;
    padding: 10px 10px 0px 10px;

    .avatar-name {
      display: flex;

      .name {
        margin-left: 10px;
        margin-top: 10px;
        font-size: 13px;
        font-weight: 600;
      }
    }

    .btn {
      margin-top: 11px;
      cursor: pointer;

      .iconfont {
        font-size: 20px;
        font-weight: bold;
      }
    }
  }
}

::-webkit-scrollbar {
  display: none;
}
</style>
